package notes;

import interfaces.Noteable;

import java.util.ArrayList;
import java.util.List;



public class NotesDatabase {

	private static List<Noteable> data = new ArrayList<Noteable>();
	
//	private NotesDatabase() {
//		data = null;
//	}
	
	public static boolean addNote(String title, String text, String time, String uriPath) {
		Noteable newNote;
		if(uriPath == null || uriPath.length() == 0)
			newNote = Note.createNewNote(title, text, time);
		else 
			newNote = NoteWithPicture.createNewNote(title, text, time, uriPath);
		if(newNote != null)
			return data.add(newNote);
		else 
			return false;
	}

	public static int getNotesCount() {
		return data.size();
	}
	
	public static Noteable getNote(int i) {
		if (!data.isEmpty()) {
			return data.get(i);
		}
		return null;
	}
	
	public static void removeNote(int i) {
		if (!data.isEmpty()) {
			data.remove(i);
		}
	}
	
	public static void editNote(String title, String text, String time, String uriPath, int position) {
		Noteable newNote;
		if(uriPath == null || uriPath.length() == 0)
			newNote = Note.createNewNote(title, text, time);
		else 
			newNote = NoteWithPicture.createNewNote(title, text, time, uriPath);
		if(newNote != null) {
			data.remove(position);
			data.add(position, newNote);
		}
		
	}
	
	public static boolean loadArray(Noteable[] notes) {
		if(notes != null && notes.length >= 1) {
			data = new ArrayList<Noteable>();
			
			int length = notes.length - 1;
			for(int i = length; i >= 0; i--) {
				data.add(notes[length - i]);
			}
			
			return true;
		}
		
		return false;
	}
	
	/**
	 *  fills the array given as a parameter with the arraylist items
	 * @param notes - must be an array referencing nothing(null)
	 * @return - if transaction is succesful
	 */
	public static boolean saveArray(Noteable[] notes) {
		if(notes != null && notes.length == getNotesCount()) {
			for(int i = notes.length - 1; i >= 0; i--) {
				notes[i] = data.get(i);
			}
			
			return notes != null;
		}
		
		return false;
	}
}
